/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function Customer() {
    this.javaClass = "limousine.vo.Customer";    
    this.customerId = "";
    this.customerType = 0;
    this.customerName = "";
    this.customerAddress = "";
    this.customerTel = "";
    this.customerEmail = "";
    this.billName = "";
    this.billAddress = "";
    this.billTel = "";    
    this.createBy = "";
    this.createtDate = "";
    this.updateBy = "";
    this.updateDate = "";
}
function ContractPerson() {
    this.javaClass = "limousine.vo.ContractPerson";
    this.customerId = "";
    this.tableId = 0;
    this.contractPerson = "";
    this.contractPersonEmail = "";   
}
function openCustomer() {
    var body = $d("_body");
    body.innerHTML = requestPage("lookup.main.jsp");
}
function edit(id) {
    try {
        formEntry();
        document.getElementById("fieldSetLabel").textContent="Customer Edit";
        var o = service.findById(id);               
                
        $d("customerId").value = o.customerId;
        $d("customerType").value = o.customerType;
        $d("customerName").value = o.customerName;
        $d("customerAddress").value = o.customerAddress;
        $d("customerTel").value = o.customerTel;
        $d("customerEmail").value = o.customerEmail;
        $d("billName").value = o.billName;
        $d("billAddress").value = o.billAddress;
        $d("billTel").value = o.billTel;        
        $d("createBy").value = o.createBy;
        $d("createDate").value = o.createDate;
        $d("updateBy").value = o.updateBy;
        $d("updateDate").value = o.updateDate;
                
        $d("bSave").setAttribute("onclick","save()");
        
        getLookupList("CUSTOMER_TYPE","customerType",o.customerType);
    } catch(err) {
        alert("edit err:"+err);
    }
}
function switchProcess(type,msg,method){
    if(type == 0){//fail
        addSysMsg(0,msg);
    }else{
        addProcessing(msg);
        setTimeout(method,2500);        
    }    
}
function save() {
    try {
        var o = new Customer();                
        o.customerId = $d("customerId").value;
        o.customerType = parseInt($d("customerType").value);
        o.customerName = $d("customerName").value;
        o.customerAddress = $d("customerAddress").value;
        o.customerTel = $d("customerTel").value;
        o.customerEmail = $d("customerEmail").value;
        o.billName = $d("billName").value;
        o.billAddress = $d("billAddress").value;
        o.billTel = $d("billTel").value;        
        o.createBy = $d("createBy").value;
        o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        o.updateDate = $d("updateDate").value;
        
        if( verify() == true ) {
            addProcessing('Please wait...');
            var ret = service.update(o);
            if( ret == "true" ) {
                switchProcess(1,"Save completed.","formSearch()");
            } else {
                switchProcess(0,"Save fail.","");    
            }
        }
    } catch(err) {
        alert("save err:"+err);
    }
}
function remove(id) {
    try {
        service.remove(id);
        search(0);
    } catch(err) {
        alert("remove err:"+err);
    }
}
function search(offset) {
    try {
        addProcessing('Please wait...');
        var ls = service.search(offset,$d("customerNameSearch").value);        
        var jTable = new JTable();
        jTable.setTableId("tableData");
        jTable.setColumns(["Type", "Name", "Address", "Telephone", "Email"]);
        jTable.setFields(["customerTypeName","customerName", "customerAddress", "customerTel", "customerEmail"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setDataId("customerId");
        jTable.setCount(ls.size());
        jTable.setOffset(offset);
        jTable.setDataPageSize(20);
        jTable.setPageLink("pagelink");
        jTable.setTableHeader();
        jTable.loadData(ls);
    } catch(err) {
        alert("search err:"+err);  
    }
    clearAllMsg();
}
function insert() {
    try {
        var o = new Customer();
        o.customerId = $d("customerId").value;
        o.customerType = parseInt($d("customerType").value);
        o.customerName = $d("customerName").value;
        o.customerAddress = $d("customerAddress").value;
        o.customerTel = $d("customerTel").value;
        o.customerEmail = $d("customerEmail").value;
        o.billName = $d("billName").value;
        o.billAddress = $d("billAddress").value;
        o.billTel = $d("billTel").value;        
        o.createBy = $d("createBy").value;
        o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        o.updateDate = $d("updateDate").value;
        
        //var re = service.save(o);
        if( verify() == true ) { 
            addProcessing('Please wait...');
            var ret = service.save(o);
            if( ret == "true" ) {
                switchProcess(1,"Add completed.","formSearch()");
            } else {
                switchProcess(0,"Add fail.","");
            }
        }
    } catch(err) {
        alert("insert err:"+err);
    }
}
        
function setInsert() {
    try {
        formEntry();
        var searchd = $d("customerId").value;
        $d("actionForm").reset();
        //$d("customerId").value = searchd;
        $d("customerId").value = service.requestId(); 
        $d("bSave").setAttribute("onclick","insert()");        
        getLookupList("CUSTOMER_TYPE","customerType","");
    } catch(err) {
        alert("setInsert err:"+err);
    }
}
         
function formEntry() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Customer Add";
    try {
        $d("formSearch").style.visibility = "hidden";
        var formEntry = $d("formEntry");
        formEntry.style.visibility = "visible";
        var panel = $d("panel");
        formEntry.style.top = panel.style.top;
        formEntry.style.left = panel.style.left;
    } catch(err) {
        alert("formEntry err:"+err);
    }
}
function formSearch() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Customer Search";    
    try {
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert("formSearch err:"+err);
    }
}
function verify() {
    try {
        var v = new Validation();
        v.addField("name='customerId',label='Customer Id',min=4,max=21,type='string',require=true");
        v.addField("name='customerType',label='Customer Type',min=0,max=11,type='int',require=true");
        v.addField("name='customerName',label='Customer Name',min=4,max=50,type='string',require=true");
        v.addField("name='customerAddress',label='Address',min=4,max=255,type='string',require=true");
        v.addField("name='customerTel',label='Telephone',min=1,max=20,type='string',require=true");
        v.addField("name='customerEmail',label='Email',min=1,max=50,type='email',require=true");
        v.addField("name='billName',label='Bill Name',min=1,max=50,type='string',require=true");
        v.addField("name='billAddress',label='Bill Address',min=4,max=255,type='string',require=true");
        v.addField("name='billTel',label='Bill Telephone',min=4,max=255,type='string',require=true");
        var str = v.execute();
        if ( str == "" ) {
            return true;
        } else {
            addSysMsg(0,str);
            return false;
        }
    } catch(err) {
        alert(err);
    }
}
function getLookupList(lookupGroupId,propertieId,selectedValue) {    
    var list = serviceLookup.getLookupType(lookupGroupId);
    var i=0;
    for( i =0; i < list.size(); i++ ) {
        var txt = list.get(i).lookupName;
        var val = list.get(i).lookupValue;
        
        if(val==selectedValue){
            $d(propertieId).options[i] = new Option(txt, val, false, true);
        }else{
            $d(propertieId).options[i] = new Option(txt, val, false, false);
        }
    }   
}